<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_customer extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_customer_model', 'IspCustomer');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Client";
		$query = $this->db->query("SELECT id_customer FROM (SELECT * FROM isp_customer ORDER BY id_customer DESC) WHERE rownum <=1");
				 				if ($query->row_array()) {
				 					$getquery = $query->row_array();
				 					$rowcount = $getquery['id_customer'];
				 					$autonum = $rowcount + 1;
				 				} else {
								$autonum = 1;
							}
		$data['kode_customer_auto'] = $autonum;
		viewByTemplate('isp_customer/index', $data, 'grid');
	}
	
	function id_auto(){
		$query = $this->db->query("SELECT id_customer FROM (SELECT * FROM isp_customer ORDER BY id_customer DESC) WHERE rownum <=1");
				 				if ($query->row_array()) {
				 					$getquery = $query->row_array();
				 					$rowcount = $getquery['id_customer'];
				 					$autonum = $rowcount + 1;
				 				} else {
								$autonum = 1;
							}
		$data['id_customer_auto'] = $autonum;
		echo json_encode($data);
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspCustomer->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_customer") == '' ) {
			$error->Add("kode_customer" , "Kode Customer", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM isp_customer WHERE kode_customer = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kode_customer"))))."'")) > 0) {
			$error->Add("kode_customer", "Kode Customer", $tpl_duplicate);
		} else if (post("nama") == '') {
			$error->Add("nama", "Nama", $tpl_empty);
		} else if (post("no_id") == '') {
			$error->Add("no_id", "Nomor KTP", $tpl_empty);
		} else if (post("alamat") == '') {
			$error->Add("alamat", "Alamat Lengkap", $tpl_empty);
		} else if (post("kota") == '') {
			$error->Add("kota", "Kota", $tpl_empty);
		} else if (post("status") == '') {
			$error->Add("status", "Status Client", $tpl_empty);
		} else if (post("tgl_survey") == '' && post("status") == 'Berlangganan') {
			$error->Add("tgl_survey", "Tanggal Survey", $tpl_empty);
		} else if (post("tgl_instalasi") == '' && post("status") == 'Berlangganan') {
			$error->Add("tgl_instalasi", "Tanggal Instalasi", $tpl_empty);
		} else if (post("tgl_aktifasi") == '' && post("status") == 'Berlangganan') {
			$error->Add("tgl_aktifasi", "Tanggal Aktifasi", $tpl_empty);
		}/* else if (post("tgl_berakhir") == '' && post("status") != 'Tidak Berlangganan')  {
			$error->Add("tgl_berakhir", "Berhenti Berlangganan", $tpl_empty);
		} *//*else if (post("tgl_trial") == '' && post("status") == 'Berlangganan') {
			$error->Add("tgl_trial", "Lama Trial", $tpl_empty);
		} else if (post("tgl_berhenti_trial") == '' && post("status") == 'Berlangganan')  {
			$error->Add("tgl_berhenti_trial", "Tanggal Berhenti Trial", $tpl_empty);
		}*/
		
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	=  array(
					'id_customer' => $this->input->post('id_customer'),
					'kode_customer' => $this->input->post('kode_customer').'-'.$this->input->post('no_customer'),
					'nama' => $this->input->post('nama'),
					'no_id' => $this->input->post('no_id'),
					'jabatan' => $this->input->post('jabatan'),
					'nama_perusahaan' => $this->input->post('nama_perusahaan'),
					'alamat' => $this->input->post('alamat'),
					'kota' => $this->input->post('kota'),
					'kode_pos' => $this->input->post('kode_pos'),
					'no_telp' => $this->input->post('no_telp'),
					'no_hp' => $this->input->post('no_hp'),
					'no_hp2' => $this->input->post('no_hp2'),
					'no_fax' => $this->input->post('no_fax'),
					'email' => $this->input->post('email'),
					'status' => $this->input->post('status'),
					'tgl_survey' => $this->input->post('tgl_survey'),
					'tgl_instalasi' => $this->input->post('tgl_instalasi'),
					'tgl_trial' => $this->input->post('tgl_trial'),
					'tgl_aktifasi' => $this->input->post('tgl_aktifasi'),
					'tgl_berakhir' => $this->input->post('tgl_berakhir'),
					'tgl_berhenti_trial' => $this->input->post('tgl_berhenti_trial'),
					);

			    $data['tgl_survey']			= ($data['tgl_survey'] == '' ? NULL : date_format3($data['tgl_survey'], 'YYYY-MM-DD'));
			    $data['tgl_instalasi']		= ($data['tgl_instalasi'] == '' ? NULL : date_format3($data['tgl_instalasi'], 'YYYY-MM-DD'));
			    $data['tgl_aktifasi']		= ($data['tgl_aktifasi'] == '' ? NULL : date_format3($data['tgl_aktifasi'], 'YYYY-MM-DD'));
			    $data['tgl_berakhir']		= ($data['tgl_berakhir'] == '' ? NULL : date_format3($data['tgl_berakhir'], 'YYYY-MM-DD'));
			    $data['tgl_berhenti_trial']	= ($data['tgl_berhenti_trial'] == '' ? NULL : date_format3($data['tgl_berhenti_trial'], 'YYYY-MM-DD'));
				
				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->IspCustomer->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//$data['modify_date']	= NOW;
					//$data['modify_user']	= USERID;
					
					$this->IspCustomer->Update($data, 
										array('id_customer'	=> $data['id_customer']));

					$ls_response = "Data Tersimpan [Edited]";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$pesan			= "Data Terhapus";

		$id_customer 	= get('id_customer');
		
		if ($id_customer != "")
		{
			// Delete Single
			$arr_rule	= array('id_customer' => $id_customer);
			$ls_result = $this->IspCustomer->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_customer' => $row['id_customer']);
				$ls_result .= $this->IspCustomer->DeleteSingle($arr_rule);
			}
		}

		echo $pesan;
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}
}
?>